package org.amphiprion.whohavethekids.query.view.builder;

import java.util.ArrayList;
import java.util.List;

import org.amphiprion.kidcalendar.entity.Configuration;
import org.amphiprion.kidcalendar.entity.Kid;
import org.amphiprion.whohavethekids.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;

public class KidsInput implements QueryInputBuilder {
	private List<CheckBox> chks = new ArrayList<CheckBox>();

	@Override
	public void build(Context context, ViewGroup root, Configuration configuration) {
		chks.clear();
		LayoutInflater inflater = LayoutInflater.from(context);
		View v = inflater.inflate(R.layout.query_input_kids, root);
		LinearLayout llkids = (LinearLayout) v.findViewById(R.id.kidList);
		for (Kid kid : configuration.getKids()) {
			CheckBox txtKid = (CheckBox) inflater.inflate(R.layout.query_input_kid_item, null);
			chks.add(txtKid);
			txtKid.setText(kid.getName());
			txtKid.setTag(kid);
			llkids.addView(txtKid);
		}
	}

	@Override
	public Object getInput() {
		List<Kid> kids = new ArrayList<Kid>();
		for (CheckBox chk : chks) {
			if (chk.isChecked()) {
				kids.add((Kid) chk.getTag());
			}
		}
		return kids;
	}

	@Override
	public boolean isInputValid() {
		// at least one kid selected
		for (CheckBox chk : chks) {
			if (chk.isChecked()) {
				return true;
			}
		}
		return false;
	}
}
